package cn.ac.siom.query.action;


import cn.ac.siom.query.common.SiomConstants;
import cn.ac.siom.query.common.WordCountTask;
import cn.ac.siom.query.dao.WordCountDao;
import cn.ac.siom.query.util.action.DefaultAction;
import cn.ac.siom.query.vo.ReturnMsg;
import cn.ac.siom.query.vo.WordCount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/wordCount")
public class WordCountController extends DefaultAction{
    public static final Logger LOGGER = LoggerFactory.getLogger(WordCountController.class);

    @Resource
    private WordCountTask wordCountTask;

    @Resource
    private WordCountDao wordCountDao;

    private static int minCount = 20;

    @RequestMapping(value = "/start", method = RequestMethod.GET)
    @ResponseBody
    public ReturnMsg start() {
        if (wordCountTask.getStatus() == WordCountTask.TASK_RUNNING) {
            return new ReturnMsg(SiomConstants.RETURN_MSG_SUCCESS, "wordCount正在统计中,请稍后重试.", null);
        }
        wordCountTask.excute();
        return new ReturnMsg(SiomConstants.RETURN_MSG_SUCCESS, "wordCount开始统计...", null);
    }

    @RequestMapping(value = "/status", method = RequestMethod.GET)
    @ResponseBody
    public ReturnMsg status(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Date date = new Date();
        List<WordCount> wordCountList = wordCountDao.queryAll();
        if (wordCountList == null || wordCountList.size() < minCount) {
            // 查询出的数量过少肯定是有问题的,取文件中的值
            try {
                insertAccessinfo(request,"知识图谱");
                File file = ResourceUtils.getFile("classpath:wordCount/backupOfwordCount.txt");
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), "UTF-8");
                BufferedReader reader = new BufferedReader(read);
                String line = "";
                List<WordCount> wordCountListBackup = new ArrayList<>();
                while ((line = reader.readLine()) != null) {
                    String[] keys = line.split(",");
                    String name = "";
                    if (keys.length > 1) {
                        for (int i = 0; i < keys.length - 1; i++) {
                            name += keys[i];
                        }
                    }
                    String count = keys[keys.length - 1];
                    int countNum = 0;
                    try {
                        countNum = Integer.valueOf(count);
                    } catch (Exception e) {

                    }
                    WordCount insetObj = new WordCount(name, countNum, date);
                    wordCountListBackup.add(insetObj);
                }
                reader.close();
                read.close();
                return new ReturnMsg(SiomConstants.RETURN_MSG_SUCCESS, "success", wordCountListBackup);
            } catch (Exception e) {
                LOGGER.error("can't find backupOfwordCount.txt", e);
                return new ReturnMsg(SiomConstants.RETURN_MSG_FAILED, "query failed", null);
            }
        } else {
            return new ReturnMsg(SiomConstants.RETURN_MSG_SUCCESS, "success", wordCountList);
        }
    }


    @Override
    public String getText() {
        return null;
    }
}
